<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/4
 * Time: 14:04
 */

namespace App\Http\Admin\Administration\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Administration\Models\ConferenceRoom;
use App\Http\Admin\Administration\Models\ConferenceRoomRecord;
use Framework\Extend\Helpers\Upload;
use Intervention\Image\Facades\Image;
class ConferenceRoomController extends Controller
{
    /**
     * 获取会议室列表
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function conferenceRoomList()
    {
        if (request()->isMethod('post')) {

            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            $searchField = [];
            $oa_company_id=session('current_operation_company_id',1);
            $searchField[] = ['oa_company_id','=',$oa_company_id];
            if (!empty($search)) {
                foreach ($search as $k => $v) {
                    if ($k == 'status'&&!is_null($v)) {
                        $v == 0 ?$searchField[] = ['end_time', '<', time()]:$searchField[] = ['end_time', '>', time()] ;
                    }
                }

                foreach ($search as $k => $v) {
                    if ($k != 'status') {
                        if ($v || $v == "0") $searchField[] = [$k, 'like', '%' . $v . '%'];
                    }
                }
            }

            $conferenceRoom = new ConferenceRoom();
            return $conferenceRoom->getConferenceRoomList($page, $pageSize, $searchField);
        } else {

            return $this->view('conference-room-list');
        }
    }

    /**
     * 录入会议室
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function conferenceRoomAdd(){
        try {
            if (request()->isMethod('post')) {
                $request = request()->all();
                $oa_company_id=session('current_operation_company_id',1);
                $request['oa_company_id'] = $oa_company_id;
                $rules = [
                    'name' => 'required',
                    'content' => 'required',
                    'main_image_url' => 'required',
                    'area' => 'required',
                    'position' => 'required',
                ];
                $this->validateRequestParameters($request, $rules, true);
                $conferenceRoom = new ConferenceRoom();
                $re = $conferenceRoom->addConferenceRoom($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                return $this->view('conference-room-add');
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    /**
     * 上传会议室图片
     * @return array
     */
    public function conferenceRoomImgUpload()
    {
        $img = request()->file('file');
        $fileInfo = Upload::uploadFile($img, 'conference-room-img');
        $imageSize = getimagesize($fileInfo['fullPath']);

        $thumbnailWidth = round($imageSize[0] / 4);
        $thumbnailHeight = round($imageSize[1] / 4);

        $thumbnailPath = getThumbnailPath($fileInfo['fullPath']);

        Upload::imageThumbnail($fileInfo['fullPath'], $thumbnailPath, $thumbnailWidth, $thumbnailHeight, 20);

//        $imgRatio = filesize($img) / 1024 / 30;
//        $imagePathArr = explode('/', $fileInfo['fullPath']);
//        $endSub = count($imagePathArr) - 1;
//        $imagePathArr[$endSub] = 'WeChat/' . $imagePathArr[$endSub];
//        $shareImgUrl = implode('/', $imagePathArr);dd($shareImgUrl);
//        $img = Image::make($shareImgUrl)->resize($thumbnailWidth, $thumbnailHeight);
//        $img->save($thumbnailPath, ceil(100 / $imgRatio));
        \DebugBar::disable();
        return [
            'statusCode' => 200,
            'filename' => '/' . $fileInfo['fullPath']
        ];
    }

    /**
     * 编辑会议室
     * @Author sxy
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function conferenceRoomEdit()
    {
        try {
            if (request()->isMethod('post')) {

                $request = request()->all();
                $rules = [
                    'name' => 'required',
                    'content' => 'required',
                    'area' => 'required',
                    'position' => 'required',

                ];
                $this->validateRequestParameters($request, $rules, true);
                $conferenceRoom = new ConferenceRoom();
                $re = $conferenceRoom->editConferenceRoom($request);
                if (!$re) return $this->ajaxFail();
                return $this->ajaxSuccess();
            } else {
                $request = request()->all();
                $id = $request['id'];
                $conferenceRoom = new ConferenceRoom();
                $conferenceInfo = $conferenceRoom->getConferenceRoomInfo($id);
                return $this->view('conference-room-edit')->with('conferenceInfo', $conferenceInfo);
            }

        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }


    /**
     * 获取会议室预约记录
     * @Author sxy
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function conferenceRoomRecordList(){


        if (request()->isMethod('post')) {
            $id=request('id');
            $page = request('pageCurrent');
            $pageSize = request('pageSize');
            $search = request()->except(['pageCurrent', 'pageSize']);
            unset($search['id']);

            $searchField = [];
            if (!empty($search)) {
                foreach ($search as $k => $v) {
                    if ($v || $v == "0") $searchField[] = [$k, 'like', '%' . $v . '%'];
                }
            }

            $conferenceRoomRecord = new ConferenceRoomRecord();
            return $conferenceRoomRecord->getConferenceRoomRecordList($page, $pageSize, $searchField,$id);
        } else {
            $id=request('id');
            return $this->view('conference-room-record-list')->with('id',$id);
        }
    }

    /**
     * 查看会议室预约记录详情
     * @Author sxy
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function conferenceRoomRecordEdit()
    {
        try {
            $request = request()->all();
            $id = $request['id'];
            $conferenceRoomRecord = new ConferenceRoomRecord();
            $conferenceInfo = $conferenceRoomRecord->getConferenceRoomInfo($id);
            $conferenceInfo['start_time']=date('Y-m-d H:i:s',$conferenceInfo['start_time']);
            $conferenceInfo['end_time']=date('Y-m-d H:i:s',$conferenceInfo['end_time']);
            return $this->view('conference-room-record-edit')->with('conferenceInfo', $conferenceInfo);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }




}